package com.sunyur.tailor.channel.service.middle.returnorder.impl;

import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.common.middle.base.ChannelDataResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelDataResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.order.model.GetOrderListItemModel;
import com.sunyur.tailor.channel.service.inner.returnorder.model.ReturnOrderHeadModel;
import com.sunyur.tailor.channel.service.inner.returnorder.model.ReturnOrderItemModel;
import com.sunyur.tailor.channel.service.inner.returnorder.param.ReturnOrderHeadParam;
import com.sunyur.tailor.channel.service.inner.returnorder.param.ReturnOrderItemParam;
import com.sunyur.tailor.channel.service.inner.returnorder.service.SunyurReturnOrderService;
import com.sunyur.tailor.channel.service.middle.returnorder.MiddleReturnOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 退货单业务接口实现
 * @Author XuHui
 * @Date 2021/7/7 14:37
 * @Version 1.0
 */
@Service("middleReturnOrderService")
public class MiddleReturnOrderServiceImpl implements MiddleReturnOrderService {

    private final static Logger LOGGER_INFO = LoggerFactory.getLogger(MiddleReturnOrderServiceImpl.class);
    private final static Logger LOGGER_ERROR = LoggerFactory.getLogger(MiddleReturnOrderServiceImpl.class);

    @Autowired
    private SunyurReturnOrderService sunyurReturnOrderService;

    @Override
    public ChannelResult<Boolean> syncReturnOrder(MsgResultCode msgResultCode) throws Exception {
        //查询退货单头和行信息
        ChannelDataResult dataResult = getHeadAndItem(msgResultCode);
        if (!ChannelDataResultUtil.isSuccess(dataResult)) {
            return ChannelResultUtil.error(dataResult.getCode(), dataResult.getMessage());
        }
        //业务处理
        ReturnOrderHeadModel returnOrderHead = (ReturnOrderHeadModel) dataResult.getHead();
        List<GetOrderListItemModel> orderListItem = (List<GetOrderListItemModel>) dataResult.getItem();

        return ChannelResultUtil.success(true);

    }

    /**
     * @return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @Author XuHui
     * @Description 查询头和行信息
     * @Date 13:56 2021/12/28
     * @Param [msgResultCode]
     **/
    private ChannelDataResult getHeadAndItem(MsgResultCode msgResultCode) throws Exception {
        //获取退货单号
        String returnOrderCode = msgResultCode.getCode();
        //查询退货头
        ChannelResult<ReturnOrderHeadModel> returnOrderResult = sunyurReturnOrderService.getReturnOrder(new ReturnOrderHeadParam(returnOrderCode));

        if (!ChannelResultUtil.isSuccess(returnOrderResult)) {
            return ChannelDataResultUtil.error(returnOrderResult.getCode(), returnOrderResult.getMessage());
        }
        //查询退货行
        ChannelListResult<ReturnOrderItemModel> returnOrderItemResult = sunyurReturnOrderService.listReturnOrderItem(new ReturnOrderItemParam(returnOrderCode));
        if (!ChannelResultUtil.isSuccess(returnOrderItemResult)) {
            return ChannelDataResultUtil.error(returnOrderItemResult.getCode(), returnOrderItemResult.getMessage());
        }

        return ChannelDataResultUtil.success(returnOrderResult.getContent(), returnOrderItemResult.getContent());
    }


}
